﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{ 
    public class CheckPointController : Controller
    {
        private MainModelContainer db = new MainModelContainer();

        static int? id;

        //
        // GET: /CheckPoint/

        public ViewResult Index(bool? edit_point, bool? delete_point, bool? edit_excursion, bool? delete_excursion,bool? show_excursion,bool? no_excursion=true)
        {
            if (no_excursion == true)
            {
                ModelState.AddModelError("", "Сначала создайте хотя бы один пункт");
            }
            if (edit_point == true)
                ViewBag.edit_point = true;
            if (delete_point == true)
                ViewBag.delete_point = true;
            if (edit_excursion == true)
                ViewBag.edit_excursion = true;
            if (delete_excursion == true)
                ViewBag.delete_excursion = true;
            if (show_excursion == true)
                ViewBag.show_excursion = true;
            return View(db.CheckPoints.ToList());
        }

        //
        // GET: /CheckPoint/Details/5

        public ViewResult Details(int id)
        {
            CheckPoint checkpoint = db.CheckPoints.Single(c => c.Id == id);
            return View(checkpoint);
        }

        //
        // GET: /CheckPoint/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /CheckPoint/Create

        [HttpPost]
        public ActionResult Create(CheckPoint checkpoint)
        {
            if (ModelState.IsValid)
            {
                db.CheckPoints.AddObject(checkpoint);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(checkpoint);
        }
        
        //
        // GET: /CheckPoint/Edit/5
 
        public ActionResult Edit(int id)
        {
            CheckPoint checkpoint = db.CheckPoints.Single(c => c.Id == id);
            return View(checkpoint);
        }

        //
        // POST: /CheckPoint/Edit/5

        [HttpPost]
        public ActionResult Edit(CheckPoint checkpoint)
        {
            if (ModelState.IsValid)
            {
                db.CheckPoints.Attach(checkpoint);
                db.ObjectStateManager.ChangeObjectState(checkpoint, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(checkpoint);
        }

        //
        // GET: /CheckPoint/Delete/5
 
        public ActionResult Delete(int id)
        {
            CheckPoint checkpoint = db.CheckPoints.Single(c => c.Id == id);
            return View(checkpoint);
        }

        //
        // POST: /CheckPoint/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            CheckPoint checkpoint = db.CheckPoints.Single(x => x.Id == id);
            CoursePoint c=(CoursePoint)(from i in db.CoursePoints where i.CheckPointRId == checkpoint.Id select i).FirstOrDefault();
            if (c!=null)
            {
                db.CoursePoints.DeleteObject(c);
            }
            List<Excursion> exc = (from i in checkpoint.Excursion select i).ToList();
            for (int i = 0; i < exc.Count; i++)
            {
                db.Excursions.DeleteObject(exc.ElementAt(i));                
            }
            db.SaveChanges();
            db.CheckPoints.DeleteObject(checkpoint);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult AddExcursion(int? exc_id)
        {
            if (exc_id != null)
            {
                id = exc_id;
            }
            else
            {
                List<string> checks = (from i in db.CheckPoints select i.Title).ToList();
                if (checks.Count == 0)
                {
                    return RedirectToAction("Index", new { no_excursion = true });
                }
                ViewBag.checks = new SelectList(checks);
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddExcursion(Excursion exc,string checks="")
        {
            CheckPoint ch;
            if (string.IsNullOrEmpty(checks))
            {
                ch = (from i in db.CheckPoints where i.Id == id select i).FirstOrDefault();
            }
            else
            {
                ch = (from i in db.CheckPoints where i.Title == checks select i).FirstOrDefault();
            }
            if (ch != null)
            {
                ch.Excursion.Add(exc);
                db.SaveChanges();
            }
            return RedirectToAction("Index", "CheckPoint");

        }
    }
}